<?php
	## insert subscreber
	function db_subscriber_insert($email)
	{
		global $wpdb;
		$table = $wpdb->prefix.'subscriber';
		$data = array('email'  => $email);
		if($wpdb->insert($table, $data))
			return true;
		return false;
	}
	
	## neu gui mai thanh cong thi moi insert moi postID nay
	function db_subscriber_post_insert($postID,$subscriberID)
	{
		global $wpdb;
		$table = $wpdb->prefix.'subscriber_post';
		$data = array('subscriber'  => $subscriberID , 'postID' => $postID);
		$wpdb->insert($table, $data);
	}
	
	####### kiem tra ton tai 
	function db_check_exists_text($table,$field,$value)
	{
		global $wpdb;
		$table = $wpdb->prefix.$table;
		$query	= "SELECT * FROM $table WHERE $field='".$value."'";
		$result	= $wpdb->get_results($query, OBJECT);
		if($result)
			return TRUE;
		else 
			return  FALSE;
	}
	
	####### kiem tra ton tai 
	function db_check_exists_int($table,$field,$value)
	{
		global $wpdb;
		$table = $wpdb->prefix.$table;
		$query	= "SELECT * FROM $table WHERE $field=$value";
		$result	= $wpdb->get_results($query, OBJECT);
		if($result)
			return TRUE;
		else 
			return  FALSE;
	}
	
	###### huy dang ky nhan tin (Xoa subscriber vaf subscreber_post)
	function db_delete_subscriber_by_email($email)
	{
		global $wpdb;
		
		$subscriberID = db_select_subscriberID_by_email($email);
		db_delete_subscriber_by_email($subscriberID);          		
		$table = $wpdb->prefix."subscriber";
		$query = "DELETE FROM $table WHERE email='".$email."'";
		$wpdb->query($query);
		
		
	}
	function db_delete_subscriber_post_by_subscreberID($id)
	{
		global $wpdb;
		$table = $wpdb->prefix."subscriber_post";
		$query = "DELETE FROM $table WHERE subscriber=$id";
		$wpdb->query($query);
	}
	
	##### Danh sach subscreber de gui mail
	function db_select_all($table)
	{
		global $wpdb;
		$table = $wpdb->prefix.$table;
		$query	= "SELECT * FROM $table";
		$result	= $wpdb->get_results($query, OBJECT);
		if($result)
			return $result;
		else 
			return false;	
	}
	
	function db_select_subscriberID_by_email($email)
	{
		global $wpdb;
		$table = $wpdb->prefix."subscriber";
		$query	= "SELECT * FROM $table WHERE email='".$email."'";
		$result	= $wpdb->get_results($query, OBJECT);
		if($result)
			return $result[0]->ID;
		else 
			return  FALSE;
	}
	
	function db_select_all_subscriber()
	{
		global $wpdb;
		$table = $wpdb->prefix."subscriber";
		$query	= "SELECT * FROM $table";
		$result	= $wpdb->get_results($query, OBJECT);
		if($result)
			return $result;
		else 
			return false;	
	}
	##### Danh sach subscreber_post da gui tin boi subscriberID
	function db_select_all_post_by_subscriberID($subscriberID)
	{
		global $wpdb;
		$table = $wpdb->prefix."subscriber_post";
		$query	= "SELECT * FROM $table WHERE subscriber=$subscriberID";
		$result	= $wpdb->get_results($query, OBJECT);
		if($result)
			return $result;
		else 
			return false;	
	}
	#####
	
?>